import React from "react";
import { Link, Routes, Route, Outlet, useParams } from "react-router-dom";

const Hot = () => {
  return <div>这是热映页</div>;
};
const Wait = () => {
  return <div>这是待映页</div>;
};
const Home = () => {
  return (
    <>
      <div>这是首页</div>
      <ul>
        <li>
          <Link to="/home">热映</Link>
        </li>
        <li>
          <Link to="/home/wait">待映</Link>
        </li>
      </ul>

      <hr />

      {/* Outlet相当于vue的router-view */}
      <Outlet />
    </>
  );
};
const About = () => {
  return <div>这是关于页</div>;
};
const Detail = () => {
  const params = useParams();
  return <div>这是详情页 - {params.id}</div>;
};

const App = () => {
  return (
    <>
      <h2>路由嵌套-v6</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/detail">详情页1</Link>
        </li>
        <li>
          <Link to="/detail/234">详情页2</Link>
        </li>
      </ul>

      <hr />

      {/* 嵌套路由就不需要再子路由写Route了 */}
      {/* 子路由里面只需要一个Outlet来存放切换的内容 */}

      {/* 在Route里面现在可以去嵌套Route */}
      <Routes>
        <Route path="/home" element={<Home />}>
          {/* <Route path="/home/hot" element={<Hot></Hot>}></Route> */}
          {/* 可以用index来代替path，下一级的路径和上一级的路径一致 */}
          <Route index element={<Hot></Hot>}></Route>
          {/* 路径不是/开头的，自动拼接到前一级的路径上 */}
          <Route path="wait" element={<Wait></Wait>}></Route>
        </Route>
        <Route path="/about" element={<About />}></Route>
        {/* 动态可选路由 */}
        <Route path="/detail" element={<Detail />}>
          <Route path=":id" element={<Detail />}></Route>
        </Route>
      </Routes>
    </>
  );
};

export default App;
